<?php
namespace app\index\controller;
use app\common\exception\SmsCode;
use app\common\exception\System as SystemException;
use app\common\exception\User as UserException;
use app\common\validate\User as UserValidate;
use app\common\exception\SmsCode as SmsCodeException;
use Session;
use Exception;
use Hook;
use Cache;
use think\captcha\Captcha;
use think\Db;

class Code extends Common
{

	public $sign = '注册驗證';
	/**
	 * 不需要登录
	 * @var array
	 */
	protected $noNeedLogin = ['login', 'register','forgetPwd'];

	/**
	 * 登录
	 */
	public function login()
	{
		if ($this->auth->member_id) {
			return $this->errorReturn(UserException::E_IS_LOGIN, UserException::$messageList[UserException::E_IS_LOGIN]);
		}

		$data = [
			'phone'      => $this->request->post('phone')
		];

		$rule = [
			'phone' => 'require'
		];

    $validate = $this->validate($data, $rule);
    if ($validate !== true) {
        return $this->errorReturn(SystemException::E_VALIDATE, $validate);
    }

    $mobile = input('post.phone');
    $codeKey = 'login:'.$mobile;
    $isExiste = Cache::store('redis')->handler()->exists($codeKey);
    if (!empty($isExiste)) {
      return $this->errorReturn(SmsCodeException::E_CODE_MANY, SmsCodeException::getErrorMsg(SmsCodeException::E_CODE_MANY));
    }
    // 生成验证码
      $randCode = mt_rand(100000, 999999);
      $content = '【亿家智慧】您的验证码：'.$randCode.'，如非本人操作，请忽略此短信。';
      $rs = general_sms($mobile, $content, 4, $randCode);
      if ($rs['respCode'] == 0000) {
        Cache::store('redis')->handler()->setex($codeKey, 60, $randCode);
        return $this->successReturn(SystemException::E_SUCCESS,SystemException::E_SUCCESS, '發送成功');
      }
      return $this->errorReturn(SystemException::E_DEFAULT, SystemException::getErrorMsg(SystemException::E_DEFAULT));
    try {
      
    } catch (SmsCodeException $SmsCodeException) {
        return $this->errorReturn($SmsCodeException->getCode(), $SmsCodeException->getMessage());
    } catch (\Exception $exception) {
        return $this->errorReturn(SystemException::E_DEFAULT, SystemException::getErrorMsg(SystemException::E_DEFAULT));
    }
	}

	/**
	 * 注册
	 */
	public function register()
	{
//		if ($this->auth->member_id) {
//			return $this->errorReturn(UserException::E_IS_LOGIN, UserException::$messageList[UserException::E_IS_LOGIN]);
//		}

		$data = [
			'phone'      => $this->request->post('phone')
		];

		$rule = [
			'phone' => 'require'
		];

    $validate = $this->validate($data, $rule);
    if ($validate !== true) {
        return $this->errorReturn(SystemException::E_VALIDATE, $validate);
    }

    $mobile = input('post.phone');
    // 判断验证码是否失效
    $codeKey = 'register:'.$mobile;
    $isExiste = Cache::store('redis')->handler()->exists($codeKey);
    if (!empty($isExiste)) {
      return $this->errorReturn(SmsCodeException::E_CODE_MANY, SmsCodeException::getErrorMsg(SmsCodeException::E_CODE_MANY));
    }

    // 生成验证码
    $randCode = mt_rand(100000, 999999);
    $content = '【亿家智慧】您的验证码：'.$randCode.'，如非本人操作，请忽略此短信。';
    $rs = general_sms($mobile, $content, 5, $randCode);
    if ($rs['respCode'] == 0000) {
      Cache::store('redis')->handler()->setex($codeKey, 300, $randCode);
      return $this->successReturn(SystemException::E_SUCCESS,SystemException::E_SUCCESS, '發送成功');
    } else {
      return $this->errorReturn(SystemException::E_DEFAULT, SystemException::getErrorMsg(SystemException::E_DEFAULT));
    }

    try {
      
    } catch (SmsCodeException $SmsCodeException) {
        return $this->errorReturn($SmsCodeException->getCode(), $SmsCodeException->getMessage());
    } catch (UserException $UserException) {
        return $this->errorReturn($UserException->getCode(), $UserException->getMessage());
    } catch (\Exception $exception) {
        return $this->errorReturn(SystemException::E_DEFAULT, SystemException::getErrorMsg(SystemException::E_DEFAULT));
    }
	}

	/**
	 * 忘记密码
	 */
	public function forgetPwd()
	{
		if ($this->auth->member_id) {
			return $this->errorReturn(UserException::E_IS_LOGIN, UserException::$messageList[UserException::E_IS_LOGIN]);
		}

		$data = [
			'phone'      => $this->request->post('phone')
		];

		$rule = [
			'phone' => 'require'
		];

        $validate = $this->validate($data, $rule);
        if ($validate !== true) {
            return $this->errorReturn(SystemException::E_VALIDATE, $validate);
        }

        $mobile = input('post.phone');
        // 判断验证码是否失效
        $codeKey = 'forgetPwd:'.$mobile;
        $isExiste = Cache::store('redis')->handler()->exists($codeKey);
        if (!empty($isExiste)) {
          return $this->errorReturn(SmsCodeException::E_CODE_MANY, SmsCodeException::getErrorMsg(SmsCodeException::E_CODE_MANY));
        }
        try {
          // 生成验证码
          $randCode = mt_rand(100000, 999999);
          $content = '【亿家智慧】您的验证码：'.$randCode.'，如非本人操作，请忽略此短信。';
          $rs =  general_sms($mobile, $content, 6, $randCode);
          if ($rs['respCode'] == 0000) {
            Cache::store('redis')->handler()->setex($codeKey, 300, $randCode);
            return $this->successReturn(SystemException::E_SUCCESS,SystemException::E_SUCCESS, '發送成功');
          } else {
            return $this->errorReturn(SmsCode::E_SEND_FAIL, SmsCode::getErrorMsg(SmsCode::E_SEND_FAIL));
          }
        } catch (SmsCodeException $SmsCodeException) {
            return $this->errorReturn($SmsCodeException->getCode(), $SmsCodeException->getMessage());
        } catch (UserException $UserException) {
            return $this->errorReturn($UserException->getCode(), $UserException->getMessage());
        } catch (\Exception $exception) {
            return $this->errorReturn(SystemException::E_DEFAULT, SystemException::getErrorMsg(SystemException::E_DEFAULT));
        }
	}

  /**
   * 支付密码
   */
  public function payPassword()
  {
    if (!$this->auth->member_id) {
      return $this->errorReturn(UserException::E_NOT_LOGIN, UserException::$messageList[UserException::E_NOT_LOGIN]);
    }

    $data = [
        'phone'      => $this->auth->phone
    ];

    $rule = [
        'phone' => 'require'
    ];

    $validate = $this->validate($data, $rule);
    if ($validate !== true) {
      return $this->errorReturn(SystemException::E_VALIDATE, $validate);
    }
    $mobile = $data['phone'];
    // 判断验证码是否失效
    $codeKey = 'pay_password:'.$mobile;
    $isExiste = Cache::store('redis')->handler()->exists($codeKey);
    if (!empty($isExiste)) {
      return $this->errorReturn(SmsCodeException::E_CODE_MANY, SmsCodeException::getErrorMsg(SmsCodeException::E_CODE_MANY));
    }
    try {
      // 生成验证码
      $randCode = mt_rand(100000, 999999);
      $content = '【亿家智慧】您的验证码：'.$randCode.'，如非本人操作，请忽略此短信。';
      $rs = general_sms($mobile, $content, 8, $randCode);

      if ($rs['respCode'] == 0000) {
        Cache::store('redis')->handler()->setex($codeKey, 300, $randCode);
        return $this->successReturn(SystemException::E_SUCCESS,SystemException::E_SUCCESS, '發送成功');
      } else {
        return $this->errorReturn(SmsCode::E_SEND_FAIL, SmsCode::getErrorMsg(SmsCode::E_SEND_FAIL));
      }
    } catch (SmsCodeException $SmsCodeException) {
      return $this->errorReturn($SmsCodeException->getCode(), $SmsCodeException->getMessage());
    } catch (UserException $UserException) {
      return $this->errorReturn($UserException->getCode(), $UserException->getMessage());
    } catch (\Exception $exception) {
      echo $exception->getMessage();exit;
      return $this->errorReturn(SystemException::E_DEFAULT, SystemException::getErrorMsg(SystemException::E_DEFAULT));
    }
  }
}

